<?php

namespace App\Http\Controllers;

use Illuminate\Support\Facades\DB;
class UkController extends Controller
{
    public function result()
    {
        dd($_POST);
        if (empty($_POST['KeyID']) )
        {
            echo  "未插入锁，或未安装锁的驱动。";
        }
        else
        {
            echo  "当前锁的ID号为：" ;
            echo $_POST['KeyID'];
        }
    }

    public function check_act(){
        $key_id = $_POST['key_id'];
        if($key_id){
            $map['key_id'] = $key_id;
            $re = db::table('usbkey')->where($map)->first();
            if($re){
                $city = getValue($re->city,'area' ,'id' ,'name' );
                $area = getValue($re->area,'area' ,'id' ,'name' );
                $org = getValue($re->org_id,'area' ,'id' ,'name' );
                if($re->type =='D'){
                    $str = '该UKEY使用范围：'.$city.$area.'；使用人群：企业用户。若不符合以上条件，请勿绑定。';
                }
                if($re->type =='P'){
                    if($re->dengji == '1'){
                        $city = '全省';
                        $dengji = '总队';
                    }
                    if($re->dengji == '2'){
                        $dengji = '支队';
                    }
                    if($re->dengji == '3'){
                        $dengji = '大队';
                    }
                    if($re->dengji == '4'){
                        $dengji = '派出所';
                    }
                    $str = '该UKEY使用范围：'.$city.$area.$org.'；使用人群：'.$dengji.'警员。若不符合以上条件，请勿绑定。';
                }
                $org = db::table('area')->where('cid',$re->area)->get();
                return ['status'=>1,'msg'=>$str,'org'=>$org];
            }else{
                return ['status'=>0];
            }
        }
    }

    public function check_reg(){
        $key_id = $_POST['key_id'];
        if($key_id){
            $map['key_id'] = $key_id;
            $re = db::table('admin_users')->where($map)->first();
            if($re){
                $username = $re->email;
                $unit_name = $re->unit_name;
                return ['status'=>1,'username'=>$username,'unit_name'=>$unit_name];
            }else{
                return ['status'=>0];
            }
        }
    }

    public function check_reg2(){
        $key_id = $_POST['key_id'];
        if($key_id){
            $map['key_id'] = $key_id;
            $map['password'] = null;
            $re = db::table('admin_users')->where($map)->first();
            if($re){
                return ['status'=>0];
            }else{
                $map2['key_id'] = $key_id;
                $user = db::table('admin_users')->where($map2)->first();
                $username = $user->email;
                return ['status'=>1,'username'=>$username];
            }
        }
    }

    public function register_qiye(){
        if($_POST){
            if(!$_POST['key_id']){
                return ['status'=> 0,'msg'=>'请插入usbkey'];
            }
            if(!$_POST['unit_name']){
                return ['status'=> 0,'msg'=>'请填写单位名称'];
            }
            if(!$_POST['org_id']){
                return ['status'=> 0,'msg'=>'请选择派出所'];
            }
            if(!$_POST['type']){
                return ['status'=> 0,'msg'=>'请填写行业类别'];
            }
            if(!$_POST['pwd1']){
                return ['status'=> 0,'msg'=>'密码不能为空'];
            }
            if(!preg_match("/^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$/",$_POST['pwd1'])){
                return ['status'=> 0,'msg'=>'密码长度为6-16位，且同时包含字母与数字'];
            }
            if(!$_POST['pwd2']){
                return ['status'=> 0,'msg'=>'确认密码不能为空'];
            }
            if($_POST['pwd1'] != $_POST['pwd2']){
                return ['status'=> 0,'msg'=>'两次输入密码不一样'];
            }
            $re = db::table('usbkey')->where('key_id',$_POST['key_id'])->first();

            if($re){
                $data['name'] = $data['unit_name'] = $_POST['unit_name'];
                $data['email'] = $re->username;
                $data['password'] = bcrypt($_POST['pwd1']);
                $data['created_at'] = date('Y-m-d H:i:s',time());
                $data['role_id'] = '22';
                $data['city'] = $re->city;
                $data['area'] = $re->area;
                $data['org_id'] = $_POST['org_id'];
                $data['key_id'] = $re->key_id;
                $data['unit_type'] = $_POST['type'];
                $data['start_time'] = date('Y-m-d H:i:s',time());
                $data['end_time'] = date('Y-m-d H:i:s',strtotime("+365 day"));
                $id = db::table('admin_users')->insertGetId($data);

                $data2['unit_name'] = $_POST['unit_name'];
                $data2['status'] = '1';
                $data2['user_id'] = $id;
                $data2['org_id'] = $_POST['org_id'];
                $data2['start_time'] = date('Y-m-d H:i:s',time());
                $data2['end_time'] = date('Y-m-d H:i:s',strtotime("+365 day"));
                db::table('usbkey')->where('key_id',$re->key_id)->update($data2);
                if($id){
                    return ['status'=> 1,'msg'=>'注册成功','email'=>$data['email'],'unit_name'=>$_POST['unit_name']];
                }
            }else{
                return ['status'=> 0,'msg'=>'系统繁忙'];
            }
        }
    }

    public function register_minjing(){
        if($_POST){
            if(!$_POST['key_id']){
                return ['status'=> 0,'msg'=>'请插入usbkey'];
            }
            if(!$_POST['name']){
                return ['status'=> 0,'msg'=>'请填写姓名'];
            }
            if(!$_POST['pwd1']){
                return ['status'=> 0,'msg'=>'密码不能为空'];
            }
            if(!preg_match("/^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$/",$_POST['pwd1'])){
                return ['status'=> 0,'msg'=>'密码长度为6-16位，且同时包含字母与数字'];
            }
            if(!$_POST['pwd2']){
                return ['status'=> 0,'msg'=>'确认密码不能为空'];
            }
            if($_POST['pwd1'] != $_POST['pwd2']){
                return ['status'=> 0,'msg'=>'两次输入密码不一样'];
            }
            $re = db::table('usbkey')->where('key_id',$_POST['key_id'])->first();

            if($re){
                $data['name'] = $_POST['name'];
                $data['password'] = bcrypt($_POST['pwd1']);
                $id = db::table('admin_users')->where('key_id',$_POST['key_id'])->update($data);

                $data2['status'] = '1';
                //$data2['start_time'] = date('Y-m-d H:i:s',time());
                //$data2['end_time'] = date('Y-m-d H:i:s',strtotime("+365 day"));
                db::table('usbkey')->where('key_id',$re->key_id)->update($data2);
                if($id){
                    return ['status'=> 1,'msg'=>'注册成功','email'=>$re->username,'name'=>$data['name']];
                }
            }else{
                return ['status'=> 0,'msg'=>'系统繁忙'];
            }
        }
    }
    
}
